package com.zzzyh.forumsystemclient.services.impl;

import com.zzzyh.forumsystemclient.services.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MailServiceImpl implements IMailService {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;
    @Override
    public void sendCodeEmail(String to, String code) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject("论坛系统验证码");
            message.setText("您好！\n\n您的验证码是: " + code + "\n\n该验证码5分钟内有效，请勿泄露给他人。\n\n感谢您的使用！");

            mailSender.send(message);
            log.info("验证码邮件发送成功，收件人: {}, 验证码: {}", to, code);
        } catch (Exception e) {
            log.error("验证码邮件发送失败，收件人: {}, 错误信息: {}", to, e.getMessage());
            throw new RuntimeException("邮件发送失败", e);
        }
    }
}
